#include "ProjectSandbox.h"
#include <imgui/imgui.h>
#include "SandboxApp.h"

ProjectSandbox::ProjectSandbox(const std::string &name, CC::Scene *scene)
	: Sandbox(name, scene)
{
	CC_INFO("-----ProjectSandbox----\n");
    //CC_INFO(" version %s", avcodec_version());
    
}

ProjectSandbox::~ProjectSandbox()
{
}

void ProjectSandbox::OnAttach()
{
    Sandbox::OnAttach();

    //m_node = GetScene()->AddImageNode(GetScene()->GetRoot(), PATHCONFIG("assets/textures/bg.png"), "bg");
    m_node = GetScene()->AddImageNode(GetScene()->GetRoot(), ("bg.png"), "bg");
    auto tran = m_node->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform);
    tran->Translation = glm::vec3(0.0, 0.0, 0.0);

    auto material = m_node->GetComponent<CC::Material2DComponent>(CC::ComponentType_Material2D);
    material->Color = glm::vec4(1.0, 1.0, 1.0, 1.0);


    {
        nodebtn = GetScene()->AddImageNode(GetScene()->GetRoot(), PATHCONFIG("assets/textures/close.png"), "close");
        auto tran = nodebtn->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform);
        tran->Translation = glm::vec3(0.0, 0.0, 0.0);
        tran->Scale = glm::vec3(20.0, 20.0, 0.0);
        auto c = nodebtn->GetComponent<CC::Material2DComponent>(CC::ComponentType_Material2D);
        c->Color = { 1.0, 1.0, 1.0, 1.0 };
    }

}

void ProjectSandbox::OnDetach()
{
    Sandbox::OnDetach();
}

void ProjectSandbox::OnUpdate(CC::Timestep ts, std::string renderscene)
{         
    auto tran = m_node->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform);
    tran->Scale = glm::vec3(GetWidth(), GetHeight(), 0.0);

    auto tran2 = nodebtn->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform);
    tran2->Translation = glm::vec3(GetWidth() - tran2->Scale[0], 0.0, 0.0);

    GetScene()->SetClearColor(m_ClearColor);
    Sandbox::OnUpdate(ts, renderscene);
}

void ProjectSandbox::OnImGuiRender()
{
    //Sandbox::OnImGuiRender();
    Layer::OnImGuiRender();
}

void ProjectSandbox::OnKeyReleased(CC::KeyReleasedEvent &e) {
    //CC_INFO("OnKeyReleased=%d\n", e.GetKeyCode());
    Sandbox::OnKeyReleased(e);
}

void ProjectSandbox::OnKeyWinPressed(CC::KeyWinEvent &e) {
    //CC_INFO("ProjectSandbox OnKeyWinPressed=%d\n", e.GetKeyCode());
    Sandbox::OnKeyWinPressed(e);
}

void ProjectSandbox::HandleLeftButtonPressed() {
    Sandbox::HandleLeftButtonPressed();
    m_ButtonLeftPressed = true;

    {
        auto t = nodebtn->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform);
        if (GetMouseX() < t->Translation[0] || GetMouseX() > t->Translation[0] + t->Scale[0]
            || GetMouseY() < t->Translation[1] || GetMouseY() > t->Translation[1] + t->Scale[1]) {
            //outside
        }
        else {
            // inside
            exit(0);
        }
    }
    

}
void ProjectSandbox::HandleLeftButtonMoved(float deltaX, float deltaY) {
    Sandbox::HandleLeftButtonMoved(deltaX, deltaY);

    if (m_ButtonLeftPressed) {

    }    
}
void ProjectSandbox::HandleLeftButtonReleased() {
    Sandbox::HandleLeftButtonReleased();
    m_ButtonLeftPressed = false;
}
void ProjectSandbox::HandleMidButtonPressed() {
    Sandbox::HandleMidButtonPressed();
    m_ButtonMidPressed = true;
}
void ProjectSandbox::HandleMidButtonMoved(float deltaX, float deltaY) {
    Sandbox::HandleMidButtonMoved(deltaX, deltaY);

    if (m_ButtonMidPressed) {
        
    }
}
void ProjectSandbox::HandleMidButtonReleased() {
    Sandbox::HandleMidButtonReleased();
    m_ButtonMidPressed = false;
}

void ProjectSandbox::OnMouseScrolled(CC::MouseScrolledEvent &e) {
    Sandbox::OnMouseScrolled(e);

    float x = e.GetXOffset();
    float y = e.GetYOffset();

}